package main

import (
	"errors"
	"fmt"
	"iot-base/common/cache"
	"iot-base/devc/common/loader"
	"main/onvifprotocol"
	"main/services"
	"strconv"
	"sync"
)

func main() {
	loader.SetParam(true, "onvif", 90, eachDev)
	loader.SetDevChangedHandler(func(tid string, pThing *cache.ThingInfo) {
		fmt.Println("---------get changed obj", tid)
		eachDev(tid, pThing, nil, 0)
	})
	go services.NewService().SubService()
	wg := sync.WaitGroup{}
	wg.Add(1)
	wg.Wait()
}

func eachDev(id string, thing *cache.ThingInfo, dev interface{}, runCount int64) (interface{}, error) {
	ip := thing.GetThingParam("ip", "")
	sport := thing.GetThingParam("port", "80")
	un := thing.GetThingParam("username", "admin")
	pswd := thing.GetThingParam("pswd", "")

	if ip == "" {
		return nil, errors.New("ip is empty")
	}
	//var port = 80

	port, err := strconv.Atoi(sport)
	if err != nil || port == 0 {
		port = 80
	}

	return onvifprotocol.ProbeOnline(dev, ip, port, un, pswd, thing, runCount)
}
